 /*
The MIT License

Copyright (c) 2009 P.J. Onori (pj@somerandomdude.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
 
package com.somerandomdude.colortoolkit.spaces
{
	import com.somerandomdude.colortoolkit.CoreColor;

	public class CMYK extends CoreColor implements IColorSpace
	{
		/* @private */
		private var _cyan:Number;
		/* @private */
		private var _yellow:Number;
		/* @private */
		private var _magenta:Number;
		/* @private */
		private var _black:Number;
		
		/**
		 * Cyan color channel
		 * @return Number (between 0 and 100)
		 * 
		 */		
		public function get cyan():Number { return this._cyan; }
		
		/**
		 * Cyan color channel
		 * @param value Number (between 0 and 100)
		 * 
		 */		
		public function set cyan(value:Number):void
		{
			this._cyan=Math.min(100, Math.max(value, 0));
			this._color=this.generateColorsFromCMYK(this._cyan, this._magenta, this._yellow, this._black);
		}
		
		/**
		 * Magenta color channel
		 * @return Number (between 0 and 100)
		 * 
		 */			
		public function get magenta():Number { return this._magenta; }
		
		/**
		 * Magenta color channel
		 * @param value Number (between 0 and 100)
		 * 
		 */	
		public function set magenta(value:Number):void
		{
			this._magenta=Math.min(100, Math.max(value, 0));
			this._color=this.generateColorsFromCMYK(this._cyan, this._magenta, this._yellow, this._black);
		}
		
		/**
		 * Yellow color channel
		 * @return Number (between 0 and 100)
		 * 
		 */			
		public function get yellow():Number { return this._yellow; }
		
		/**
		 * Yellow color channel
		 * @param value Number (between 0 and 100)
		 * 
		 */	
		public function set yellow(value:Number):void
		{
			this._yellow=Math.min(100, Math.max(value, 0));
			this._color=this.generateColorsFromCMYK(this._cyan, this._magenta, this._yellow, this._black);
		}
		
		/**
		 * Black color channel
		 * @return Number (between 0 and 100)
		 * 
		 */			
		public function get black():Number { return this._black; }
		
		/**
		 * Black color channel
		 * @param value Number (between 0 and 100)
		 * 
		 */	
		public function set black(value:Number):void
		{
			this._black=Math.min(100, Math.max(value, 0));
			this._color=this.generateColorsFromCMYK(this._cyan, this._magenta, this._yellow, this._black);
		}
		
		/**
		 * Hexidecimal RGB translation of CMYK color
		 * @return Hexidecimal color value
		 * 
		 */		
		public function get color():int { return this._color; }
		
		/**
		 * Hexidecimal RGB translation of CMYK color
		 * @param value Hexidecimal color value
		 * 
		 */		
		public function set color(value:int):void
		{
			this._color=value;
			var cmyk:CMYK = this.generateColorsFromHex(value);
			this._cyan=cmyk.cyan;
			this._magenta=cmyk.magenta;
			this._yellow=cmyk.yellow;
			this._black=cmyk.black;
		}
		
		/**
		 * 
		 * @param cyan Number (between 0 and 100)
		 * @param magenta Number (between 0 and 100)
		 * @param yellow Number (between 0 and 100)
		 * @param black Number (between 0 and 100)
		 * 
		 */		
		public function CMYK(cyan:Number=0, magenta:Number=0, yellow:Number=0, black:Number=0)
		{
			this._cyan=Math.min(100, Math.max(cyan, 0));
			this._magenta=Math.min(100, Math.max(magenta, 0));
			this._yellow=Math.min(100, Math.max(yellow, 0));
			this._black=Math.min(100, Math.max(black, 0));
			this._color=this.generateColorsFromCMYK(_cyan, _magenta, _yellow, _black);
		}
		
		public function clone():IColorSpace { return new CMYK(_cyan, _magenta, _yellow, _black); }
		
		/* @private */
		private function generateColorsFromHex(color:int):CMYK
		{
			var r:Number = color >> 16 & 0xFF;
			var g:Number = color >> 8 & 0xFF;
			var b:Number = color & 0xFF; 
			
			
			var c:Number;
			var m:Number;
			var y:Number;
			var k:Number;
			var var_K:Number;
			
			 c = 1 - ( r / 255 );
			 m = 1 - ( g / 255 );
			 y = 1 - ( b / 255 );
			var_K = 1;
			
			if ( c < var_K )   var_K = c;
			if ( m < var_K )   var_K = m;
			if ( y < var_K )   var_K = y;
			if ( var_K == 1 ) { //Black
			   c = 0;
			   m = 0;
			   y = 0;
			}
			else {
			   c = ( c - var_K ) / ( 1 - var_K ) *100;
			   m = ( m - var_K ) / ( 1 - var_K ) *100;
			   y = ( y - var_K ) / ( 1 - var_K ) *100;
			}
			 k = var_K*100;
			 
			 
			 return new CMYK(c,m,y,k);
		}	
		
		/* @private */
		private function generateColorsFromCMYK(cyan:Number, magenta:Number, yellow:Number, black:Number):int
		{
			
			cyan = Math.min(100, cyan + black);
			magenta = Math.min(100, magenta + black);
			yellow = Math.min(100, yellow + black);
			
			var r:Number = (100-cyan)*(255/100);
			var g:Number = (100-magenta)*(255/100);
			var b:Number = (100-yellow)*(255/100);
			
			return (r << 16 ^ g << 8 ^ b);
		}	
	}
}